/**
* @Title: caseAuditList_Ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	var app = angular.module('fwa.mainApp');
	app.controller('ProblemManagementListCtrl', ['$scope','$state','$http','$modal','CaseAuditListService','CaseCommonService',
	                                           'BaseURL','Modal','FwaTabsetService','AuditDictServices','InsuranceService',
	                                function($scope,$state,$http,$modal,caseAuditListService,CaseCommonService,
	                                		baseUrl,Modal,fwaTabsetService,AuditDictServices,InsuranceService){
		$scope.addOnClick = function(){
			fwaTabsetService.selectTab('caseAudit.caseAuditAdd',null,false);
		}
		
		InsuranceService.initMedicalOrgInfoList().success(function(response){
            $scope.medicalOrgInfoList = response.medicalOrgList;
		});
		
		$scope.auditDictServices = AuditDictServices;
		AuditDictServices.initCodeListDatas(); 

        var cellTemplate = 
            '<a ng-click="operationDispathch(data);">分配</a>';
		$scope.columns = [
  		{
            label : '事件号',
            map : 'problemNo',
            width : '10%',
            cellTemplate:'<a  ng:bind="data.problemNo" '+
           'ng-click="queryByProblemCode(data)"></a>'},
        {
			label : '事件时间段',
			map : 'periodDate',
			width : '15%'
		}, {
			label : '事件要点',
			map : 'keyPoint',
			width : '20%'
		}, {
			label : '来源方式',
			map : 'sourceTypeDesc',
			width : '10%'
		}, {
			label : '提交日期',
			map : 'submitDate',
			width : '15%'
		}, {
			label : '关联案件号',
			map : 'otherCaseCode',
			width : '10%'
		}, {
			label : '状态',
			map : 'statusName',
			width : '10%'
		}, {
			label : '操作',
			map : '1',
			width : '10%',
			cellTemplate:cellTemplate}
        ];
		
        $scope.operationDispathch = function(data){
            $state.go('problemManagement.auditDispatch',{problemNo:data.problemNo});
        }
        
        $scope.queryByProblemCode = function(data){
            $state.go("problemDetailView.detail",{problemCode:data.problemNo,problemStatus:data.problemStatus});
        }
        
        
        //************批量操作begin**************
        /**
         * 获取勾选的总记录数
         */
        $scope.checkRows = [];
        
        function findCheck(d) {
        	for(var i=0;i <$scope.checkRows.length; i++) {
                 var checkedData = $scope.checkRows[i];
                 if(checkedData.problemNo == d.problemNo) {
                     return i;
                 }
             }
             return -1;
         }

         $scope.$on('fwatable.checked', function(e,d){
             if(findCheck(d) == -1) {
                 $scope.checkRows.push(d);
             }
         });

         $scope.$on('fwatable.unchecked', function(e,d){
             var index = findCheck(d);
             if(index != -1) {
                 $scope.checkRows.splice(index,1);
             }
         });
    
     /**
      * 事件管理批量提交
      */
      $scope.batchSumit = function(){
    	  $scope.auditObj = {};
    	  if($scope.checkRows.length>0){
    		    var flag = false;
    		 	for(var i = 0;i< $scope.checkRows.length;i++) {
                    var checkedData = $scope.checkRows[i];
                    var caseStatus = checkedData.problemStatus;
                    if(caseStatus != '1'){//1--事件待分配
                    	flag = true;
                    }
                }
    		if(flag){
    		 		 Modal.alert("只能选择同一状态进行批量操作！");
           		     return;
    		}else{
    			var batchProblemCode=new Array();
			     for(var i=0; i<$scope.checkRows.length; i++) {
			    		var checkedData =  $scope.checkRows[i];
			    		batchProblemCode.push(checkedData.problemNo);
			 	 }
			     var obj={
			    		 problemCodes:batchProblemCode
			     };
			     submitSelectAllRecord(obj, $scope);
    		}
    		 
    	  }else{
    		  Modal.alert("请勾选要批量提交的记录！");
    		  return;
    	  }
      }
     
     /**
      * 开始提交批量选择的记录
      */
     function submitSelectAllRecord(parameter, $scope){
    	 $http({
				method : 'post',
				cache : false,
				url : baseUrl + 'auditDispatch/problemManagementBatchSubmit.do',
				params :{
					problemCodes:parameter.problemCodes
				}
			}).success(function(data) {
				if(data!=null && data.state){
					Modal.alert(data.msg);
					if(data.state=='true'){
						$scope.onSubmit();
					}
				}
			});
     }
     //************批量操作end**************
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        

        /**
         * 稽核
         * @param data
         */
        $scope.projectAudit = function(data){
            fwaTabsetService.selectTab('caseAuditProject',{problemNo:data},false);
        }
        /**
         * 调查
         * @param data
         */
		$scope.dealPorblemNo = function(data){
			fwaTabsetService.selectTab('auditDispatchResult',{problemNo:data},false);
		}
        /**
         * 复核
         * @param data
         */
        $scope.reviewProblemNo = function(data){
            fwaTabsetService.selectTab('auditDispatchReviewResult',{problemNo:data},false);
        }
        /**
         * 查看复核详情
         * @param data
         */
        $scope.reviewProblemNoDetail = function(data){
            fwaTabsetService.selectTab('auditDispatchReviewDetailResult',{problemNo:data},false);
        }
        /**
         * 查看调查详情
         * @param data
         */
        $scope.dealPorblemNoDetail = function(data){
            fwaTabsetService.selectTab('auditDispatchDetailResult',{problemNo:data},false);
        }
		
		$scope.onReset = function(){
            $scope.params.problemCode=null;
            $scope.params.orgName=null;
            $scope.params.hearDate={start:null,end:null};
            $scope.params.sourceType=null;
            $scope.params.doctorName=null;
            $scope.params.jobLicense=null;
            $scope.params.sbmitDate={start:null,end:null};
	        };

            $scope.currentPage = 1;
            var start = 0;
            $scope.limit = 20;
            $scope.params = {
                page:0,
                pageSize:20
            };

            $scope.$on('pageTool.skipPage',function(e,v){
                $scope.currentPage = v;
                start = $scope.limit*(v-1);
                $scope.onSubmit(true);
            });

	    $scope.onSubmit = function(resetPage){
            
            if(resetPage == null || !resetPage){
	    		$scope.currentPage= 1;
	    		$scope.params.page = 0; 
	    	}else{
    			$scope.params.page = ($scope.currentPage - 1) * 20;
	    	}
            
            if($scope.params.problemCode == ""){
            	$scope.params.problemCode = null;
            }
            var fparam = $scope.params;
            if($scope.params.hearDate){
                var hearDateStart = $scope.params.hearDate.start;
                var hearDateEnd = $scope.params.hearDate.end;
                fparam.hearDateStart = hearDateStart;
                fparam.hearDateEnd = hearDateEnd;
            }
            if($scope.params.sbmitDate){
                var start = $scope.params.sbmitDate.start;
                var end = $scope.params.sbmitDate.end;
                fparam.start = start;
                fparam.end = end;
            }
	    	fparam.problemStatus ="\'1\'";
            CaseCommonService.loadProblemByStatus(fparam).success(function(response){
	    		if(response){
            		$scope.tableData = response.root;
        			$scope.totalCount = response.total;
	    		}
	    	});
	    }
	    
	    $scope.onSubmit();
	}]);
})();
